#include "./status_task.h"
#include "../ice_driver/sensor.h"
#include "../ice_driver/ctrl.h"
#include "../pub_driver/cmd_can.h"
#include "./make_ice_task.h"
#include "./draw_task.h"

namespace ice_task {

StatusTask statusTask;

using namespace ice_driver;
using namespace pub_driver;
using namespace conn;

static constexpr uint16_t VERSION = 0x05;

void StatusTask::run() {

    for (;;) {

        sensor.read_temp();

        uint8_t sw1 = sensor.is_ice_full() |
            (sensor.is_bucket_high() << 1) |
            (sensor.is_bucket_low() << 2) |
            (sensor.is_leak() << 3) |
            (sensor.is_env_ok() << 4) |
            (sensor.is_evap_ok() << 5);

        cmdCAN.notify(
            addr::IPC,
            proto::ice::STATUS_UPLOAD,
            VERSION,
            sw1,
            sensor.stir_speed(),
            sensor.env_temp(),
            sensor.evap_temp(),
            ctrl.state(),
            drawTask.state(),
            makeIceTask.state()
        );

        osDelay(500);
    }
}

}
